package com.ego.product.web;

import com.alibaba.fastjson.TypeReference;
import com.alipay.api.AlipayApiException;
import com.ego.common.constant.OrderConstant;
import com.ego.common.constant.SystemConstant;
import com.ego.common.to.order.OrderDetailTo;
import com.ego.common.util.Result;
import com.ego.common.vo.order.OrderSubmitVo;
import com.ego.product.component.AlipayTemplate;
import com.ego.product.feign.OrderFeign;
import com.ego.product.interceptor.LoginInterceptor;
import com.ego.product.service.OrderService;
import com.ego.product.vo.order.PayBeforeVo;
import com.ego.product.vo.order.OrderConfirmVo;
import com.ego.product.vo.order.PayVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.concurrent.ExecutionException;

/**
 * @program: ego
 * @description: 订单
 * @author: KamTang
 * @create: 2022-12-20 11:46
 **/
@Controller
@RequestMapping("/order")
@Slf4j
public class OrderController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderFeign orderFeign;
    @Autowired
    private AlipayTemplate alipayTemplate;

    /**
     * 订单确认页
     */
    @GetMapping("/confirm")
    public String confirm(Model model) throws ExecutionException, InterruptedException {
        OrderConfirmVo orderConfirm = orderService.confirm();
        model.addAttribute("orderConfirm", orderConfirm);
        return "order-settle";
    }

    /**
     * 提交订单
     */
    @PostMapping("/submit")
    @ResponseBody
    public Result submit(@Valid @RequestBody OrderSubmitVo submit) {
        submit.setMemberId(LoginInterceptor.threadLocal.get().getId());
        submit.setMemberUserName(LoginInterceptor.threadLocal.get().getUsername());
        submit.setPayType(OrderConstant.PayType.ZFB.getType());
        Result result = orderFeign.submit(submit);
        if (result.getCode() == SystemConstant.Status.SUCCESS.getCode()) {
            OrderDetailTo orderDetailTo = result.getData("orderCreateTo", new TypeReference<OrderDetailTo>() {
            });
            return Result.ok().put("data", orderDetailTo);
        } else {
            return result;
        }
    }

    @GetMapping("/pay-select")
    public String payPage(@RequestParam("orderSn") String orderSn,
                          @RequestParam("payPrice") String payPrice, Model model) {
        PayBeforeVo payBeforeVo = new PayBeforeVo();
        payBeforeVo.setOrderSn(orderSn);
        payBeforeVo.setPayPrice(new BigDecimal(payPrice));
        if (ObjectUtils.isNotEmpty(payBeforeVo)) {
            model.addAttribute("submitOrderResp", payBeforeVo);
            return "pay-select";
        } else {
            return "order-settle";
        }
    }

    /**
     * 支付
     */
    @GetMapping(value = "/pay", produces = "text/html")
    @ResponseBody
    public String pay(@RequestParam("orderSn") String orderSn) {
        Result payInfo = orderFeign.getPayInfo(orderSn);
        PayVo payVo = payInfo.getData("payVo", new TypeReference<PayVo>() {
        });
        try {
            return alipayTemplate.pay(payVo);
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        return "order-settle";
    }

    /**
     * 订单详情
     */
    @GetMapping("/{orderSn}")
    public String detail(@PathVariable("orderSn") String orderSn, Model model) {
        Result result = orderFeign.detail(orderSn);
        OrderDetailTo orderDetail = result.getData(new TypeReference<OrderDetailTo>() {
        });
        model.addAttribute("orderDetailVo", orderDetail);
        return "order-detail";
    }
}
